﻿using System;
using Fwql.Net.Utils;

namespace Fwql.Net.Extensions
{
    public static class ArrayExtension
    {
        ///<summary>
        ///	检查Array是否为空
        ///</summary>
        ///<param name = "source"></param>
        ///<returns></returns>
        public static bool IsNullOrEmpty(this Array source)
        {
            return source == null || source.Length <= 0;
        }

        ///<summary>
        ///	检查索引是否越界
        ///</summary>
        ///<param name = "source"></param>
        ///<param name = "index"></param>
        ///<returns></returns>
        public static bool IndexOutOfBound(this Array source, int index)
        {
            Guard.NotNull(source, "source");
            return index < 0 || index >= source.Length;
        }

        /// <summary>
        /// 检查索引是否在范围之内
        /// </summary>
        /// <param name="source"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static bool IndexWithinBound(this Array source, int index)
        {
            return !(IndexOutOfBound(source, index));
        }

        /// <summary>
        /// 检查索引是否越界
        /// </summary>
        /// <param name="source"></param>
        /// <param name="index"></param>
        /// <param name="dimension">维度</param>
        /// <returns></returns>
        public static bool IndexOutOfBound(this Array source, int index, int dimension = 0)
        {
            Guard.NotNull(source, "source");
            return index < source.GetLowerBound(dimension) || index > source.GetUpperBound(0);
        }

        ///<summary>
        ///	检查索引是否在范围之内
        ///</summary>
        ///<param name = "source"></param>
        ///<param name = "index"></param>
        ///<param name="dimension">维度</param>
        ///<returns></returns>
        public static bool IndexWithinBound(this Array source, int index, int dimension = 0)
        {
            return !(IndexOutOfBound(source, index, dimension));
        }
    }
}